const api = require("../../../api/sop");
const { toast } = require("../../../utils/wxapi");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    realName: "",
    idCardNo: "",
    imgFront: "",
  },
  upload() {
    wx.showLoading({ title: "上传中" });
    wx.chooseMedia({
      count: 1,
      sourceType: ["album"],
      mediaType: ["image"],
      success: (res) => {
        const file = res.tempFiles[0].tempFilePath;
        const temp = new Date().valueOf();
        wx.cloud.uploadFile({
          cloudPath: "idcard/" + temp + ".png",
          filePath: file,
          success: (res) => {
            this.setData({
              imgFront: res.fileID,
            });
            toast("上传成功");
          },
          fail: (err) => {
            err(err);
            toast("上传失败");
          },
        });
      },
    });
  },
  async sub() {
    wx.showLoading({ title: "加载中" });
    try {
      const { realName, idCardNo, imgFront } = this.data;
      if (!realName || !idCardNo || !imgFront) {
        return toast("请填写完整信息");
      }
      const data = JSON.stringify({ realName, idCardNo, imgFront });
      const res = await api.real(data);
      console.log(res);
      if (res.respCode === 200) {
        let user = wx.getStorageSync("user");
        user.realState = 1;
        wx.setStorageSync("user", user);
        toast("认证成功");
        setTimeout(() => {
          wx.navigateBack({ delta: 1 });
        });
      } else {
        toast(res.msg);
      }
    } catch (error) {
      err(error);
      toast("认证失败");
    }
  },
});
